package cl.rizepe.playntest.core;

import static playn.core.PlayN.assets;
import static playn.core.PlayN.graphics;

import java.util.HashMap;

import playn.core.CanvasImage;
import playn.core.Color;
import playn.core.Game;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Pointer.Event;
import playn.core.gl.GroupLayerGL;
import pythagoras.f.Point;
import cl.rizepe.playntest.core.GestureManager.Gesture;
import cl.rizepe.playntest.core.effects.EffectMenu;
import cl.rizepe.playntest.core.effects.EffectPaint;
import cl.rizepe.playntest.core.effects.EffectedListener;
import cl.rizepe.playntest.core.effects.Shape;
import cl.rizepe.playntest.core.listeners.DragCopy;
import cl.rizepe.playntest.core.listeners.DragMove;
import cl.rizepe.playntest.core.listeners.DragRotate;
import cl.rizepe.playntest.core.listeners.DragZoom;
import cl.rizepe.playntest.core.listeners.ListenerAdaptor;
import cl.rizepe.playntest.core.menu.MenuDirectionClock;
import cl.rizepe.playntest.core.views.EffectedImageLayerGL;
import cl.rizepe.playntest.core.views.EffectedLayer;
import cl.rizepe.playntest.core.views.LayerProperties;
import cl.rizepe.playntest.core.views.LayerPropertyAnimation;
import cl.rizepe.playntest.core.views.Sprite;
import cl.rizepe.playntest.core.views.SpriteComposed;
import cl.rizepe.playntest.core.views.SpriteComposedDirection;
import cl.rizepe.playntest.core.views.SpriteGL;
import cl.rizepe.playntest.core.views.UpdateableGroupLayer;

public class Juego implements Game {
	static final float GRAVITY = 64;
	public enum AvailableImages{skeletonSlash,skullIcon, bodyMale,bodySkeleton,block,blockgreen, background, armorTorsoPlate, armorPantsPlate,armorShoulderPlate};
	EffectedLayer EffectedLayer;
	 Sprite layer2;
	  float px, py;
	  float x, y;
	  float vx, vy;
	  float ax, ay;
	  float rotation,rotation2;
	  UpdateableGroupLayer layerList;
	  HashMap<AvailableImages,Image> imageMap = new HashMap<AvailableImages,Image>();
	  LayerPropertyAnimation lpa;
	  float time=0;
	  CanvasImage pointsImage;
	  ImageLayer pointsLayer;
	  GroupLayerGL.Clipped window;
	  
	  void initPoints() {
	    pointsImage = graphics().createImage(50, 50);
	    pointsLayer = graphics().createImageLayer(pointsImage);
	    pointsLayer.setTranslation(50, 50);
	    pointsLayer.setScale(3f);
	    window.add(pointsLayer);
	    pointsLayer.setVisible(true);
	  }
	  
	 /* Inicializa el tamaño de la pantalla según ancho y alto.
	  * Si la pantalla es más pequeña (como en mi xperia active)
	  * inicializa la pantalla en un tamaño menor,
	  * manteniendo las proporciones entregadas, y escalando todo lo dibujado
	  * Es por esto que rootLayer() no debería ser manipulado.
	  * */
	  public void initializeScreenSize(int width, int height){
		  graphics().setSize(width, height);
		  Point screenSize = new Point(graphics().screenWidth(),graphics().screenHeight());
		  if (screenSize.x()<width){
			  float ratio=(float) (screenSize.x()/width);
			  graphics().setSize((int) (screenSize.x()), (int) (height*ratio));
			  graphics().rootLayer().setScale(ratio);
			  }
		  if  (screenSize.y()<height){
			  float ratio=(float) (screenSize.y()/height);
			  graphics().setSize((int) (screenSize.y()), (int) (height*ratio));
			  graphics().rootLayer().setScale(ratio);
		  } 
	  }  	  
	  
	  
	  public void init() {

		  this.initializeScreenSize(800	, 600);
		  
		    {Image img = assets().getImage("images/block2.png");
		    imageMap.put(AvailableImages.blockgreen,img);
		    
		    Image bgImg = assets().getImage("images/lotr.jpg");
		   imageMap.get(AvailableImages.background);
		    imageMap.put(AvailableImages.background, bgImg);
		      Image imageSkull=assets().getImage("images/skull_and_crossbones_small.png");
		    imageMap.put(AvailableImages.skullIcon,imageSkull);
		    imageMap.put(AvailableImages.bodyMale, assets().getImage("images/BODY_male.png"));
		    imageMap.put(AvailableImages.bodySkeleton, assets().getImage("images/BODY_skeleton.png"));
		    imageMap.put(AvailableImages.armorShoulderPlate, assets().getImage("images/TORSO_plate_armor_arms_shoulders.png"));
		    imageMap.put(AvailableImages.armorTorsoPlate, assets().getImage("images/TORSO_plate_armor_torso.png"));
		    imageMap.put(AvailableImages.armorPantsPlate, assets().getImage("images/LEGS_plate_armor_pants.png"));
		    imageMap.put(AvailableImages.skeletonSlash, assets().getImage("images/slash/BODY_skeleton.png"));
		  } 
		    while (!imageMap.get(AvailableImages.skullIcon).isReady())
		    {
		    	
		    }String[] pics={"images/chaos1.png",
	    			"images/chaos2.png",
	    			"images/chaos3.png",
	    			"images/chaos4.png",
	    			"images/chaos5.png",
	    			"images/heroes1.png",
	    			"images/heroes2.png",
	    			"images/heroes3.png",
	    			"images/heroes4.png",
	    			"images/heroes5.png",
	    			"images/ork1.png",
	    			"images/ork2.png",
	    			"images/ork3.png",
	    			"images/ork4.png",
	    			"images/ork5.png"
	    			};
		  
		  
		  
		  

			  layerList = new UpdateableGroupLayer(graphics().ctx(),600,600);	
		 
		//  layerList.setTranslation(layerList.width() / 2,layerList.height()/ 2);
		//  layerList.setOrigin(layerList.width() / 2, layerList.height() / 2);
		  
	//	layerList.setTranslation(300,300);
	//	 layerList.setOrigin(300, 300);
		   window=new  GroupLayerGL.Clipped(graphics().ctx(), 600, 600);
		  graphics().rootLayer().add(window);
		  window.add(layerList);
		ImageLayer lay=graphics().createImageLayer(imageMap.get(AvailableImages.blockgreen)); 
	    EffectedLayer = new EffectedImageLayerGL(lay); 
	    final EffectedImageLayerGL backgroundLayer = new EffectedImageLayerGL(graphics().createImageLayer(imageMap.get(AvailableImages.background)));
	    layerList.add(backgroundLayer);
	//    backgroundLayer.setTranslation(backgroundLayer.getSize().x() / 2,backgroundLayer.getSize().y()/ 2);
	 //   backgroundLayer.setOrigin(backgroundLayer.getSize().x() / 2, backgroundLayer.getSize().y() / 2);
	    GestureManager gestures =new GestureManager ();
	    DragZoom dz = new DragZoom (backgroundLayer, backgroundLayer.image()){
	    	@Override 
	    	public void onPointerEnd(Event event){
	    		layerList.setScale(df/di*layerList.transform().uniformScale());
	    			//zoom(df/di*layerList.transform().uniformScale(), layerList);
	    			layerShadow.setVisible(false);
	    			//layerList.setSize(600/layerList.transform().uniformScale(), 600/layerList.transform().uniformScale());
    		}	    	
	    };
	    gestures.addGestureListener(Gesture.onDragStart, dz);
	    gestures.addGestureListener(Gesture.onDragMove, dz);
	    gestures.addGestureListener(Gesture.onDragEnd, dz);
	    
	    backgroundLayer.addListener(gestures);
	    ListenerAdaptor lis= new ListenerAdaptor(){
	    	@Override
	    	public void onPointerEnd(Event event){
	    		float scale=layerList.transform().uniformScale();
	    		float windowWidth = window.width()/scale;
	    		float windowHeight=window.height()/scale;
	    		float backgroundWidth=backgroundLayer.width();
	    		float x = event.localX()-windowWidth/2;
	    		x= x>backgroundWidth-windowWidth?backgroundWidth-windowWidth:x;
	    		x = x <0 ? 0:x;
	    		float y = event.localY()-windowHeight/2;

	    		y= y>backgroundLayer.height()-windowHeight?backgroundLayer.height()-windowHeight:y;

	    		y=y<0?0:y;
	    		layerList.setOrigin(x, y);
	    		//layerList.setTranslation(event.localX(), y);
	    		//backgroundLayer.setOrigin(x/2, -y/2);
	    		//layerList.setTranslation(x, y);
	    		System.out.println("eventX:"+event.localX()+
	    				"   scale*eventX:"+event.localX()/scale+
	    				"   scale:"+scale+
	    				"   finalX:"+x+
	    				"   windowWidth"+windowWidth+
	    				"   backgroundWidth"+backgroundWidth);
	    	}
	    };
	    ListenerAdaptor paner = new ListenerAdaptor(){
	    	@Override
	    	public void onPointerEnd(Event event){
	    		layerList.setTranslation(layerList.transform().tx()+50, layerList.transform().ty()+50);
	    	}
	    };
	    gestures.addGestureListener(Gesture.onClick,lis);
	    gestures.addGestureListener(Gesture.onDoubleClick,lis);
	    
	    layerList.add(lay);
	    
	    x = graphics().width() / 2;
	    y = graphics().height() / 2;
	    DragMove dragListener=new DragMove(EffectedLayer,imageMap.get(AvailableImages.blockgreen),0.1f);
	   
	    gestures =new GestureManager ();
	    gestures.addGestureListener(Gesture.onDragStart, dragListener);
	    gestures.addGestureListener(Gesture.onDragMove, dragListener);
	    gestures.addGestureListener(Gesture.onDragEnd, dragListener);
	    EffectedLayer.addListener(gestures);
	    
	    Shape shape=new Shape(EffectedLayer, Color.rgb(255, 255, 0),0.5f);
	    shape.apply();
	    EffectedLayer.setTranslation(EffectedLayer.getSize().x() / 2,EffectedLayer.getSize().y()/ 2);
	    EffectedLayer.setOrigin(EffectedLayer.getSize().x() / 2, EffectedLayer.getSize().y() / 2);
	    
	  

		  initPoints();
	    for (int i=0;i<13;i++){
	    	SpriteComposed spriteTemp =  new SpriteComposed(imageMap.get(AvailableImages.blockgreen),8,1);
		   
		    spriteTemp.setOrigin(spriteTemp.getWidth() / 2, spriteTemp.getHeight() / 2);
		    int frames=graphics().width()/spriteTemp.getWidth();
		    spriteTemp.setTranslation(spriteTemp.getWidth() / 2+(i%frames)*spriteTemp.getWidth(), spriteTemp.getHeight() / 2+(i/frames)*spriteTemp.getWidth());
		    GestureManager gm = new GestureManager();
		    
		    
		    
		  //  LayerState ls= new LayerState(spriteTemp.imageLayer);
    		DragMove dm = new DragMove (spriteTemp,spriteTemp.getImage(),0.1f);
		    gm.addGestureListener(Gesture.onDragStart, dm);
		    gm.addGestureListener(Gesture.onDragMove, dm);
		    gm.addGestureListener(Gesture.onDragEnd, dm);
		    spriteTemp.addListener(gm);
		    
		    
		    
		    if (i==0){
		    	createCopiableLayer(imageMap.get(AvailableImages.skullIcon), spriteTemp, gm, dm);
		    }
		    if (i==1){
		    	createRotableLayer(imageMap.get(AvailableImages.skullIcon), spriteTemp, gm);
		    }
		    if (i==2)
		    {		    	
		    	createZoomableLayer(imageMap.get(AvailableImages.skullIcon), spriteTemp, gm);			    
		    }
		    if (i==3)
		    {		
			    createPaintableLayer(imageMap.get(AvailableImages.skullIcon), spriteTemp, gm);
		    }
		    if (i==4)
		    {		    	
		    	createDoubleClickableLayer(spriteTemp, gm);
		    }
		    if (i==5)
		    {		    	
		    	createClickableLayer(spriteTemp, gm);
		    }

		    if (i==6){
		    	createFixedMenuLayer(spriteTemp, gm, pics);
		    }
		    if (i==7){
		    	createRadialMenuLayer(spriteTemp, gm, pics);
		    }
		    if (i==8){
		    	 createAnimatedSprite(spriteTemp, gm);
		    	
		    }
		    if (i==10){


		    	spriteTemp = createComposedSprite(i, gm);
		    }
		    if (i==11){
		    	 spriteTemp = createComposedSpriteLayer(i, gm);
		    }
		    if (i==12){
		    /*	*/ 
		    	 
		    	 final SpriteComposedDirection sprite2 =  new SpriteComposedDirection(imageMap.get(AvailableImages.skeletonSlash),6,4);
		    	// sprite2.initRowSprite(sprite2, 1);
		 		layerList.add(sprite2);
		 		sprite2.setFrecuency(0);
		 		sprite2.setRow(1);
				dm = new DragMove (sprite2,sprite2.getImage(),0.1f);
				gm.addGestureListener(Gesture.onDragStart, dm);
				gm.addGestureListener(Gesture.onDragMove, dm);
				gm.addGestureListener(Gesture.onDragEnd, dm);
		 		sprite2.addListener(gm);
		 		sprite2.setScale(0.5f);

		 		gm.addGestureListener(Gesture.onClick, new ListenerAdaptor(){
		 			@Override
		 			public void onPointerEnd(Event event) {
						
						sprite2.advanceFrame();
					}
		 		});
		 		gm.addGestureListener(Gesture.onDoubleClick, new ListenerAdaptor(){
		 			@Override
		 			public void onPointerEnd(Event event) {
						
						sprite2.advanceFrame();
					}
		 		});/**/
		    }
		    layerList.add(spriteTemp);
	    }
	    /*
	    layer.setTranslation(
	  	      x+ layer.image().width() / 2,
	  	      y+ layer.image().height() / 2
	  	    );
	    /* x = 0;
	    y = 0;
	    /*
	    x = graphics().width() / 2;
	    y = graphics().height() / 2;
	    /*x=0+img.width()/2;
	    y=0;*/
	    ay = GRAVITY; 
	    rotation =0;
	    rotation2 =0;

		//graphics().rootLayer().setScale(2);
	//	GroupLayerState root = new GroupLayerState(graphics().rootLayer());
		
	  }




	protected SpriteComposed createComposedSprite(int i, GestureManager gm) {
		SpriteComposed spriteTemp;
		int frames;
		DragMove dm;
		spriteTemp =  new SpriteComposed(imageMap.get(AvailableImages.bodySkeleton),9,4);
		spriteTemp.addImage(imageMap.get(AvailableImages.armorPantsPlate));
		spriteTemp.addImage(imageMap.get(AvailableImages.armorTorsoPlate));
		spriteTemp.addImage(imageMap.get(AvailableImages.armorShoulderPlate));
		spriteTemp.setFrecuency(0);
		dm = new DragMove (spriteTemp,spriteTemp.getImage(),0.1f);
		gm.addGestureListener(Gesture.onDragStart, dm);
		gm.addGestureListener(Gesture.onDragMove, dm);
		gm.addGestureListener(Gesture.onDragEnd, dm);
		spriteTemp.addListener(gm);
		layerList.add(spriteTemp);
		spriteTemp.setOrigin(spriteTemp.getWidth() / 2, spriteTemp.getHeight() / 2);
		frames=graphics().width()/spriteTemp.getWidth();
		spriteTemp.setTranslation(spriteTemp.getWidth() / 2+(i%frames)*spriteTemp.getWidth(), spriteTemp.getHeight() / 2+(i/frames)*spriteTemp.getWidth());
		final SpriteComposed spriteTemp2=spriteTemp;
		gm.addGestureListener(Gesture.onClick, new ListenerAdaptor(){	
			@Override
			public void onPointerEnd(Event event) {
				if (spriteTemp2!=null)
					spriteTemp2.removeImage(spriteTemp2.imagesSize()-1);
				spriteTemp2.advanceFrame();
			}
		}
		);
		spriteTemp.addListener(gm);
		return spriteTemp;
	}




	protected SpriteComposed createComposedSpriteLayer(int i, GestureManager gm) {
		SpriteComposed spriteTemp;
		int frames;
		DragMove dm;
		spriteTemp =  new SpriteComposed(imageMap.get(AvailableImages.bodySkeleton),9,4);

		    
		spriteTemp.setFrecuency(5);
		spriteTemp.addImage(imageMap.get(AvailableImages.armorPantsPlate));
		spriteTemp.addImage(imageMap.get(AvailableImages.armorTorsoPlate));
		spriteTemp.addImage(imageMap.get(AvailableImages.armorShoulderPlate));
		layerList.add(spriteTemp);
		spriteTemp.setOrigin(spriteTemp.getWidth() / 2, spriteTemp.getHeight() / 2);
		frames=graphics().width()/spriteTemp.getWidth();
		spriteTemp.setTranslation(spriteTemp.getWidth() / 2+(i%frames)*spriteTemp.getWidth(), spriteTemp.getHeight() / 2+(i/frames)*spriteTemp.getWidth());
		final SpriteComposed spriteTemp1=spriteTemp;
		Shape shapi=new Shape(spriteTemp, Color.rgb(255, 0, 0),0.5f);
		EffectedListener el= new EffectedListener(shapi);
		gm.addGestureListener(Gesture.onDoubleClick, el);
		dm = new DragMove (spriteTemp1,spriteTemp1.getImage(),0.1f);
		gm.addGestureListener(Gesture.onDragStart, dm);
		gm.addGestureListener(Gesture.onDragMove, dm);
		gm.addGestureListener(Gesture.onDragEnd, dm);
		spriteTemp.addListener(gm);
		gm.addGestureListener(Gesture.onClick, new ListenerAdaptor(){	
			@Override
			public void onPointerEnd(Event event) {
				if (spriteTemp1!=null)
					spriteTemp1.removeImage(spriteTemp1.imagesSize()-1);
			}
		}
		);
		return spriteTemp1;
	}

	  


	protected void createAnimatedSprite(Sprite spriteTemp, GestureManager gm) {
		lpa = new LayerPropertyAnimation(spriteTemp,3);
		Point translate= spriteTemp.getTranslate();
		translate=translate.add(-250, 50);
		LayerProperties end=lpa.getEnd();
		end.setTranslate(translate);
		lpa.setEnd(end);
		lpa.setLoop(true);
		gm.addGestureListener(Gesture.onClick, new ListenerAdaptor(){
			@Override
			public void onPointerEnd(Event event) {
				lpa.startAnimation();
			}
		});
	}




	protected void createRadialMenuLayer(Sprite spriteTemp, GestureManager gm,
			String[] pics) {
		EffectMenu em = new EffectMenu(spriteTemp,5,5);
		em.getMenu().setDirection(
				new MenuDirectionClock(
						em.getMenu().getOrigin().getMenuOrigin(),
						em.getMenu().getButtonSize(),
						1,2));		
		for (int j=0;j<6;j++){
			ImageLayer il=graphics().createImageLayer(assets().getImage(pics[j]));			
			layerList.add(il);
			em.getMenu().addButton(il);
			em.getMenu().erase();			
		}		
		EffectedListener el= new EffectedListener(em);
		gm.addGestureListener(Gesture.onClick, el);
		
	}

	protected void createFixedMenuLayer(Sprite spriteTemp, GestureManager gm,
			String[] pics) {
		EffectMenu em = new EffectMenu(spriteTemp,3,9);		
		for (int j=0;j<2;j++){
			ImageLayer il=graphics().createImageLayer(assets().getImage(pics[j]));
			layerList.add(il);
			em.getMenu().addButton(il);
			em.getMenu().erase();			
		}		
		EffectedListener el= new EffectedListener(em);
		gm.addGestureListener(Gesture.onClick, el);
	}

	protected void createClickableLayer(Sprite spriteTemp, GestureManager gm) {
		Shape shapi=new Shape(spriteTemp, Color.rgb(255, 0, 0),0.5f);
		EffectedListener el= new EffectedListener(shapi);
		gm.addGestureListener(Gesture.onClick, el);
	}

	protected void createDoubleClickableLayer(Sprite spriteTemp, GestureManager gm) {
		Shape shapi=new Shape(spriteTemp, Color.rgb(255, 0, 0),0.5f);
		EffectedListener el= new EffectedListener(shapi);
		gm.addGestureListener(Gesture.onDoubleClick, el);
	}

	protected void createPaintableLayer(Image imageSkull, Sprite spriteTemp,
			GestureManager gm) {
		EffectPaint effPaint=new EffectPaint(spriteTemp,8,3,3,imageSkull);    	
		EffectedListener el= new EffectedListener(effPaint);
		gm.addGestureListener(Gesture.onDoubleClick, el);
		gm.addGestureListener(Gesture.onClick, el);
	}

	protected void createZoomableLayer(Image imageSkull, SpriteGL spriteTemp,
			GestureManager gm) {
		DragZoom dz = new DragZoom (spriteTemp,spriteTemp.image());
		gm.addGestureListener(Gesture.onDragStart, dz);
		gm.addGestureListener(Gesture.onDragMove, dz);
		gm.addGestureListener(Gesture.onDragEnd, dz);

		EffectPaint effPaint=new EffectPaint(spriteTemp,15,4,4,imageSkull);
		EffectedListener el= new EffectedListener(effPaint);
		gm.addGestureListener(Gesture.onClick, el);
	}

	protected void createRotableLayer(Image imageSkull, Sprite spriteTemp,
			GestureManager gm) {
		EffectPaint effPaint=new EffectPaint(spriteTemp,0,2,2, imageSkull);
		EffectedListener el= new EffectedListener(effPaint);
		gm.addGestureListener(Gesture.onDoubleClick, el);
		gm.addGestureListener(Gesture.onClick, el);
		DragRotate dr = new DragRotate (spriteTemp,spriteTemp.getImage(),0.4f);
		gm.addGestureListener(Gesture.onDragStart, dr);
		gm.addGestureListener(Gesture.onDragMove, dr);
		gm.addGestureListener(Gesture.onDragEnd, dr);
	}

	protected void createCopiableLayer(Image imageSkull, SpriteGL spriteTemp,
			GestureManager gm, DragMove dm) {
		GestureManager forCopyGM= new GestureManager();
		
		forCopyGM.addGestureListener(Gesture.onDragStart, dm);
		forCopyGM.addGestureListener(Gesture.onDragMove, dm);
		forCopyGM.addGestureListener(Gesture.onDragEnd, dm);
	//	EffectPaint effPaint=new EffectPaint(spriteTemp,0,2,2, imageSkull);
		//EffectedListener el= new EffectedListener(effPaint);
		//forCopyGM.addGestureListener(Gesture.onClick, el);
		//Shape shapi=new Shape(spriteTemp, Color.rgb(255, 0, 0),0.5f);
		
		//forCopyGM.addGestureListener(Gesture.onDoubleClick, new EffectedListener(shapi));
		
		final SpriteGL finalSprite=spriteTemp.clone();
		DragCopy dc = new DragCopy (spriteTemp,
				spriteTemp.getImage(),
				0.1f, 
				layerList,
				new GestureManager.GestureManagerFactory() {					
					@Override
					public GestureManager createGestureManager(EffectedLayer effectedLayer) {
						DragMove dragListener=new DragMove(effectedLayer,finalSprite.image(),0.1f);
					    final GestureManager gestures =new GestureManager ();
					    
					    gestures.addGestureListener(Gesture.onDragStart, dragListener);
					    gestures.addGestureListener(Gesture.onDragMove, dragListener);
					    gestures.addGestureListener(Gesture.onDragEnd, dragListener);
					    
					    Image imageSkull=assets().getImage("images/skull_and_crossbones_small.png");
					    EffectPaint effPaint=new EffectPaint(EffectedLayer,0,2,2, imageSkull);
				    	EffectedListener el= new EffectedListener(effPaint);
				    	Shape shapi=new Shape(effectedLayer, Color.rgb(255, 0, 0),0.5f);
				    	gestures.addGestureListener(Gesture.onClick, el);
				    	gestures.addGestureListener(Gesture.onDoubleClick, new EffectedListener(shapi));
					    
				    	
				    	/*final EffectedLayer layer=effectedLayer;
					   
					      final int[] effectNumber={0};
					    ListenerAdaptor lis = new ListenerAdaptor(){
					    	@Override
					    	public void onPointerEnd(Event event){
					    		if (effectNumber[0]==0)
					    			{
					    			DragMove dragListener=new DragMove(layer,finalSprite.image(),0.1f);
					    			gestures.addGestureListener(Gesture.onDragStart, dragListener);
								    gestures.addGestureListener(Gesture.onDragMove, dragListener);
								    gestures.addGestureListener(Gesture.onDragEnd, dragListener);
					    			}
					    		if (effectNumber[0]==1)
				    			{
				    			DragRotate dragListener=new DragRotate(layer,finalSprite.image(),0.1f);
				    			gestures.addGestureListener(Gesture.onDragStart, dragListener);
							    gestures.addGestureListener(Gesture.onDragMove, dragListener);
							    gestures.addGestureListener(Gesture.onDragEnd, dragListener);
				    			}
					    		if (effectNumber[0]==2)
				    			{
				    			DragZoom dragListener=new DragZoom(layer,finalSprite.image());
				    			gestures.addGestureListener(Gesture.onDragStart, dragListener);
							    gestures.addGestureListener(Gesture.onDragMove, dragListener);
							    gestures.addGestureListener(Gesture.onDragEnd, dragListener);
				    			}
					    		effectNumber[0]=effectNumber[0]++%3;
					    	}
					    };
				    	gestures.addGestureListener(Gesture.onClick, lis);
				    	/**/
				    						    
						return gestures;
					}
				})
		{
			@Override
			public void cloneLayer(Event event){
				SpriteGL sprite=finalSprite.clone();
				sprite.setTranslation(newPostion(event).x(),
						newPostion(event).y());
				if (gestureManagerFactory!=null){
					sprite.addListener(gestureManagerFactory.createGestureManager(sprite));
					}
				layerList.add(sprite);
			}
			
		};
		spriteTemp.setScale(0.8f);
		gm.addGestureListener(Gesture.onDragStart, dc);
		gm.addGestureListener(Gesture.onDragMove, dc);
		gm.addGestureListener(Gesture.onDragEnd, dc);
	}

	  public void update(float delta) {
	    // Save previous position for interpolation.
	    px = x;
	    py = y;

	    // Update physics.
	    delta /= 4000;
	    vx += ax * delta;
	    vy += ay * delta;
	    x += vx * delta;
	    y += vy * delta;
	    rotation += delta*4;
	    for (int i=0;i<layerList.size();i++){
		    rotation2 += delta;
	    }
	    layerList.update(delta);
	    time+=delta;
	  }

	  public void paint(float alpha) {
	    // Interpolate current position.
	//    float x = (this.x * alpha) + (px * (1f - alpha));
	 //   float y = (this.y * alpha) + (py * (1f - alpha));

	    // Update the layer.
	/**   layer.setTranslation(
	      x - layer.image().width() / 2,
	      y - layer.image().height() / 2
	    );/**/
	  //  layer.setRotation(rotation);.
		  for (int i=0;i<layerList.size();i++){
			  if (i==7|i==6|i==7|i==5|i==9  )
		  {
	    	layerList.get(i).setRotation(rotation2);
	    }
		    	//layerList.get(i).update(alpha);
		  }

	//	  
		    String s = Float.toString(time/3);
			  lpa.update(alpha/updateRate());
		    pointsImage.canvas().clear();
		    pointsImage.canvas().setFillColor(0xff000000);
		    pointsImage.canvas().drawText(s, 10f, 10f);
	  }

	  public int updateRate() {
	    return 30;
	  }
	  
	  
}